# Maintainer: Jonathan Armstrong <jda2158@gmail.com>

options=(debug !strip)

_realname=heimdall
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=r180.3997d5c
pkgrel=2
_commit='3997d5cc607e6c603c6e7c0d07e42e9868c62af2'
pkgdesc="Heimdall is a cross-platform open-source tool suite used to flash firmware (aka ROMs) onto Samsung mobile devices."
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://glassechidna.com.au/heimdall/"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
          "${MINGW_PACKAGE_PREFIX}-libusb"
          "${MINGW_PACKAGE_PREFIX}-qt5-base")
makedepends=("${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "git")

source=("heimdall"::"git+https://github.com/Benjamin-Dobell/Heimdall#commit=$_commit"
        0001-fix-clang.patch)
sha256sums=('SKIP'
            'f3404e6ec9bdae948845ccf220c8653578df81841f403bc2fd2c654b2327a044')


pkgver() {
    cd "$srcdir/heimdall"
    printf 'r%s.%s' "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd heimdall
  patch -p1 -i "${srcdir}/0001-fix-clang.patch"
}

build() {

  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
     ../heimdall
  ${MINGW_PREFIX}/bin/cmake --build .
}


package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}/${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
